<!--
 * @Author: zhangmengqiong
 * @Date: 2021-05-12 14:18:00
 * @LastEditors: zhangmengqiong
 * @LastEditTime: 2021-05-13 11:05:48
-->
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script
      type="text/javascript"
      src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=DVPI4kwxgMqXDrTx483rHkB7c3KDRyPp"
    ></script>
    <script
      type="text/javascript"
      src="https://api.map.baidu.com/library/TrackAnimation/src/TrackAnimation_min.js"
    ></script>
    <title>百度地图轨迹动画</title>
    <style type="text/css">
      html {
        width: 100%;
        height: 100%;
      }
      body {
        width: 100%;
        height: 100%;
        margin: 0px;
        padding: 0px;
      }
      #container {
        width: 100%;
        height: 100%;
      }
      .tools {
        top: 10px;
        left: 10px;
        position: absolute;
        z-index: 99;
      }
    </style>
  </head>

  <body>
    <div id="container"></div>
    <div class="tools">
      <button id="start">开始</button>
      <button id="cancel">取消</button>
    </div>
    <script type="text/javascript">
      var map = new BMapGL.Map('container');
      // 创建地图实例
      var point = new BMapGL.Point(116.404, 39.915);
      // 创建点坐标
      map.centerAndZoom(point, 18);
      // 通过滚轮进行放大缩小
      map.enableScrollWheelZoom(true);
      // 轨迹动画,在工具库中，第一个参数map对象 lines 数组
      var points = [
        new BMapGL.Point(116.418038, 39.91979),
        new BMapGL.Point(116.418267, 40.059246),
        new BMapGL.Point(116.307899, 40.057038),
      ];

      var lines = new BMapGL.Polyline(points);
      var opts = {
        delay: 1000,
        duration: 10000,
        tilt: 30, // 轨迹动画播放产生的倾角
        overallView: true, // 自动调整视角
      };
      // 动画事件
      document.getElementById('start').addEventListener('click', function (e) {
        // 开启动画
        trackAnimation.start();
      });
      document.getElementById('cancel').addEventListener('click', function (e) {
        // 开启动画
        trackAnimation.cancel();
      });
      var trackAnimation = new BMapGLLib.TrackAnimation(map, lines, opts);
    </script>
  </body>
</html>
